package com.netx.cubigraf.apps.utilities;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import com.netx.cubigraf.shared.ApplicationProps;
import com.netx.generics.basic.DefaultLogger;
import com.netx.generics.basic.Logger;
import com.netx.generics.io.FileSystem;
import com.netx.generics.io.Location;
import com.netx.generics.util.CsvLogger;


public class Window {

	// TYPE SECTION:
	public static void main(String[] args) {
		try {
			Display display = new Display();
			Shell shell = new Shell(display, SWT.CLOSE | SWT.MIN);
			Window window = new Window(shell, args, new PRJ15_ZerarCompras());
			window.toString();
			shell.open();
			while(!shell.isDisposed()) {
				if(!display.readAndDispatch()) {
					display.sleep();
				}
			}
			display.dispose();
		}
		catch(Throwable t) {
			System.out.print("Unexpected error: ");
			t.printStackTrace(System.out);
		}
	}

	// INSTANC SECTION:
	private final Logger _logger;

	public Window(Shell shell, final String[] args, final Utility u) {
		// Init Logger:
		Logger tmp = null;
		try {
			tmp = new CsvLogger(new String[]{"Message"}, new FileSystem(new Location(ApplicationProps.getEapHome()+"\\04.Logs")), "utilities_$now('yyyyMMdd_HHmmss')");
		}
		catch(IOException io) {
			System.err.println("Could not open LOG file.");
			io.printStackTrace(System.err);
			tmp = new DefaultLogger();
		}
		_logger = tmp;
		
		final int s_c_margin=20;
		final int s_c_height=25, s_c_width=220, s_c_sep=10;
		int s_y_pos=20;
		int s_w_width = s_c_width+s_c_margin*2;

		shell.setText("Utilities");
		shell.setBounds(50, 50, s_w_width, 160);
		Image img = new Image(shell.getDisplay(), getClass().getResourceAsStream("Icon.ico"));
		shell.setImage(img);

		// Utility name:
		Label label1 = new Label(shell, SWT.CENTER);
		label1.setText("Utility: "+u.getClass().getSimpleName());
		label1.setBounds(s_c_margin, s_y_pos+3, s_c_width, s_c_height);
		s_y_pos = s_y_pos + s_c_height + s_c_sep;

		// Message label:
		final Label label2 = new Label(shell, SWT.CENTER);

		// Button:
		final Button startButton = new Button(shell, SWT.NONE);
		startButton.setText("Start");
		startButton.setToolTipText("Click to start the utility process");
		startButton.setBounds(s_w_width/2-80/2, s_y_pos, 80, s_c_height);
		startButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent se) {
				try {
					startButton.setEnabled(false);
					u.run(args);
					label2.setText("Process finished");
				}
				catch(Throwable t) {
					_logger.error("Error running utility", t);
				}
			}
		});
		s_y_pos = s_y_pos + s_c_height + s_c_sep;
		
		// Message label:
		label2.setBounds(s_c_margin, s_y_pos+3, s_c_width, s_c_height);
	}

}
